package org.framework.customutil;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.poi.ss.formula.functions.T;

public class ListUtils {

	/**
	 * 按新的Class对象复制
	 * 
	 * @param source list, example: list
	 * @param dest class, example: UserEntity.class;
	 */
	public static <E> List<E> copyTo(List<?> source, Class<E> destinationClass)
			throws Exception {
		if (source.size() == 0)
			return new ArrayList<>();
		List<E> res = new ArrayList<E>(source.size());
		for (Object o : source) {
			E e = destinationClass.newInstance();
			BeanUtils.copyProperties(e, o);
			res.add(e);
		}
		return res;
	}
	
	/**
	 * @Description: 拆分list数组，按照指定长度 .<br>
	 * @param oldList 旧的数组.<br>
	 * @param length 指定长度.<br>
	 * @author 郑成功 .<br>
	 * @date 2018-5-29 下午5:35:59.<br>
	 */
	public static List<T> splitArrays(List<T> oldList,int length){
		List<T> list = new ArrayList<T>();
		int startIndex = 0;
		while (oldList.size()>startIndex) {
			if(oldList.size()-startIndex>length){
				list.addAll(list.subList(startIndex, startIndex+6));
			}else{
				list.addAll(list.subList(startIndex, oldList.size()));
			}
		}
		return list;
	}
	
	public static boolean isNullOrEmpty(List list){
		return list == null || list.isEmpty();
	}
}
